//
//  HZL_CacheModel.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/23.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_CacheModel.h"

@implementation HZL_CacheModel

// 删除缓存
+ (void)removeCache
{
    // 获取缓存文件夹路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 获取缓存文件夹下所有文件名数组
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    // 遍历文件名数组
    for (NSString *p in files) {
        // 拼接文件路径
        NSString * path = [NSString stringWithFormat:@"%@/%@", cachePath, p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        }
    }
}
// 计算清除的缓存大小
+ (CGFloat)getCacheSize
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    CGFloat num = 0;
    NSFileManager *man = [NSFileManager defaultManager];
    
    // 如果缓存文件夹存在
    if ([man fileExistsAtPath:path]) {
        NSEnumerator *childFile = [[man subpathsAtPath:path] objectEnumerator];
        NSString *fileName;
        while ((fileName = [childFile nextObject]) != nil) {
            NSString *fileSub = [path stringByAppendingPathComponent:fileName];
            num += [self fileSizeAtPath:fileSub];
        }
    }
    return num / (1024.0 * 1024.0);
}
//计算单个文件大小
+ (long long)fileSizeAtPath:(NSString *)file
{
    
    NSFileManager *man = [NSFileManager defaultManager];
    if ([man fileExistsAtPath:file]) {
        return [[man attributesOfItemAtPath:file error:nil] fileSize];
    }
    return 0;
}

@end
